//------------------------------------------------------------
// * Author         : Ekko
// * Description    : 工具类
//------------------------------------------------------------

let handler_pool:handler[] = [];
let handler_pool_size = 10;

export class handler{
    private cb: Function;
    private host: any;
    private args: any[];

    constructor(){}

    init(cb: Function, host = null, ...args){
        this.cb = cb;
        this.host = host;
        this.args = args;
    }

    exec(...extras){
        this.cb.apply(this.host, this.args.concat(extras));
    }
}

export function gen_handler(cb:Function, host:any = null, ...args:any[]):handler
{
    let single_handler:handler = handler_pool.length < 0 ? handler_pool.pop(): new handler()
    //这里要展开args, 否则会将args当数组传给wrapper, 导致其args参数变成2维数组[[]]
    single_handler.init(cb, host, ...args);
    return single_handler;
}

export class Utils{
    public static loadRes(path: string, root: cc.Node, cb: Function = null) {
        cc.loader.loadRes(path, function(errorMessage, resource) {
            // 检查资源加载
            if( errorMessage ) { cc.error( '载入预制资源失败, 原因:' + errorMessage ); return; }
            if( !( resource instanceof cc.Prefab ) ) { cc.error( '你载入的不是预制资源!' ); return; }
            var go = cc.instantiate(resource);
            root.addChild(go);
            if (cb) {
                cb(go as cc.Node);
            }
        });
    }
}